package com.wms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmDateTiemDto;
import com.wms.dto.ConfirmUnloadTiemDto;
import com.wms.mapper.WarehousingEntryDetailsMapper;
import com.wms.pojo.PageResult;
import com.wms.pojo.WarehousingEntryDetails;
import com.wms.pojo.WarehousingEntryDetailsSort;
import com.wms.service.WarehousingEntryDetailsService;
import com.wms.service.WarehousingEntryDetailsSortService;
import com.wms.vo.WarehousingDetailsVo;
import com.wms.vo.WarehousingEntryDetailsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class WarehousingEntryDetailsServiceImpl extends ServiceImpl<WarehousingEntryDetailsMapper, WarehousingEntryDetails> implements WarehousingEntryDetailsService {
    @Autowired
    private WarehousingEntryDetailsSortService sortService;

    @Override
    public PageResult queryPage(Integer page, Integer pageSize, String supplierName) {
        PageHelper.startPage(page,pageSize);
        List<WarehousingEntryDetailsVo> voList = getBaseMapper().queryPage(supplierName);
        Page<WarehousingEntryDetailsVo> list = (Page<WarehousingEntryDetailsVo>) voList;
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    @Transactional
    public void updateByIdStatus(ConfirmDateTiemDto dto) {
        List<Integer> list = dto.getList();
        if (ObjectUtil.isNotEmpty(list)){
            list.forEach(id->{
                WarehousingEntryDetails warehousingEntryDetails = WarehousingEntryDetails.builder()
                        .id(id)
                        .realArrivalTime(dto.getDateTime())
                        .status(2).build();
                getBaseMapper().updateById(warehousingEntryDetails);
            });
        }
    }

    //查询卸货
    @Override
    public PageResult queryUnloadPage(Integer page, Integer pageSize, String supplierName) {
        PageHelper.startPage(page,pageSize);
        List<WarehousingEntryDetailsVo> voList = getBaseMapper().queryUnloadPage(supplierName);
        Page<WarehousingEntryDetailsVo> list = (Page<WarehousingEntryDetailsVo>) voList;
        return new PageResult(list.getTotal(),list.getResult());
    }

    @Override
    @Transactional
    public void updateUnloadById(ConfirmUnloadTiemDto dto) {
        List<Integer> list = dto.getList();
        if (ObjectUtil.isNotEmpty(list)){
            list.forEach(id->{
                WarehousingEntryDetails warehousingEntryDetails = WarehousingEntryDetails.builder()
                        .id(id)
                        .unloadEmpId(dto.getUserId())
                        .unloadTime(dto.getDateTime())
                        .status(3).build();
                getBaseMapper().updateById(warehousingEntryDetails);
                //查询明细数据
                WarehousingEntryDetails details = getBaseMapper().selectById(id);
                //给分拣表添加数据
                WarehousingEntryDetailsSort sort = WarehousingEntryDetailsSort.builder()
                        .warehousingEntryDetailsId(details.getId())
                        .status(1)
                        .count(0)
                        .skuId(details.getSkuId())
                        .createTime(LocalDateTime.now())
                        .updateTime(LocalDateTime.now())
                        .build();
                sortService.save(sort);
            });
        }
    }

    @Override
    public PageResult getDetailsAll(Integer page, Integer pageSize, String supplierName) {
        PageHelper.startPage(page,pageSize,supplierName);
        List<WarehousingDetailsVo> list = getBaseMapper().getDetailsAll(supplierName);
        Page<WarehousingDetailsVo> voPage = (Page<WarehousingDetailsVo>) list;
        return new PageResult(voPage.getTotal(),voPage.getResult());
    }
}
